#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QLabel>
#include <QString>
#include <fstream>
#include <QAction>
#include <QMenuBar>
#include <QToolBar>
#include <QTextEdit>
#include <QStatusBar>
#include <QDockWidget>
#include <QPushButton>
#include <QFileDialog>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QMenuBar *menubar = new QMenuBar(this);
    this->setMenuBar(menubar);

    QMenu *menu1 = new QMenu("file");
    QMenu *menu2 = new QMenu("Edit");

    menubar->addMenu(menu1);
    menubar->addMenu(menu2);

    QAction *action1 = new QAction("Save");
    QAction *action2 = new QAction("Load");

    QAction *openAction = new QAction("open", this);
    QAction *newAction = new QAction("new", this);

    menu1->addAction(action1);
    menu1->addSeparator();
    menu1->addAction(action2);

    QToolBar *toolBar1 = new QToolBar(this);
    QToolBar *toolBar2 = new QToolBar(this);
    // 这里指定初始显示位置在左侧还是右侧，工具栏是可以拖动的
    // this->addToolBar(Qt::LeftToolBarArea, toolBar1);
    // this->addToolBar(Qt::RightToolBarArea, toolBar2);
    toolBar1->addAction(openAction);
    toolBar1->addSeparator();
    toolBar1->addAction(newAction);
    QPushButton *btn = new QPushButton("保存", this);
    toolBar1->addWidget(btn);
    this->addToolBar(toolBar1);
    this->addToolBar(toolBar2);
    // 这里是限定工具栏可以出现的位置，如果是只限定在左侧出现的话(竖着)，他在原始位置的时候是一个长条
    // 如果拖动到右侧就变成一个小方块了
    /**
     * setAllowedAreas() 方法设置的是工具栏的"停靠区域"(dock areas),也就是工具栏可以固定停靠的位置。但这并不会完全阻止工具栏被拖动到其他位置。
     *当工具栏被拖到不允许停靠的区域时:
     *工具栏会变成一个浮动的小窗口(floating window)
     *不能在该区域停靠(dock)
     *显示为一个小方块形状
     */
    toolBar1->setAllowedAreas(Qt::LeftToolBarArea);
    toolBar2->setAllowedAreas(Qt::RightToolBarArea);
    // 是否可以从主窗口拖出去，得到一个独立的窗口
    toolBar1->setFloatable(true);
    toolBar2->setFloatable(false);

    toolBar1->addAction("action1");
    toolBar2->addAction("action2");
    // 设置为false的话，设置其停靠位置的操作就不会生效
    toolBar1->setMovable(true);
    toolBar2->setMovable(true);

    QTextEdit *edit = new QTextEdit(this);
    this->setCentralWidget(edit);
    edit->setPlaceholderText("Edit the text content here");

    QStatusBar *stabar = new QStatusBar(this);
    this->setStatusBar(stabar);
    // stabar->showMessage("hello", 5000);

    QLabel *label1 = new QLabel("hint mes", this);
    stabar->addWidget(label1);

    QLabel *label2 = new QLabel("right hint mes", this);
    stabar->addPermanentWidget(label2);

    QLabel *label3 = new QLabel("hint mes 1", this);
    stabar->addWidget(label3);

    QDockWidget *dockwidget = new QDockWidget("float window", this);
    this->addDockWidget(Qt::BottomDockWidgetArea, dockwidget);

    QAction *action3 = new QAction("action3", dockwidget);
    QToolBar *dockbar = new QToolBar(dockwidget);
    dockbar->addAction(action3);
    dockwidget->setWidget(dockbar);

    dockwidget->setAllowedAreas(Qt::TopDockWidgetArea | Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea);

    connect(action1, &QAction::triggered, this, [this, edit]()
            /**/
            {
        QFileDialog *dialog = new QFileDialog(this);
        QString filename = dialog->getSaveFileName(this, "Save", "C:\\Users\\lenovo1\\Desktop>");
        qDebug("filename: %s", filename.toStdString().c_str());
        
        if (filename.isEmpty())
            return;
        QFile file(filename);
        if (file.exists())
        {
            QMessageBox::StandardButton reply;
            reply = QMessageBox::question(this, "File exists", "File already exists. Do you want to overwrite it?",
                                    QMessageBox::Yes|QMessageBox::No);
            if (reply == QMessageBox::No)
                return ;
        }

        std::ofstream outfile;
        outfile.open(filename.toStdString().c_str());
        if (!outfile.is_open())
        {
            QMessageBox::critical(this, "Error", "Failed to save file");
            return;
        }
        const QString &text = edit->toPlainText();
        outfile << text.toStdString();
        outfile.close(); 
        QMessageBox::information(this, "SUccess", "File saved successfully");
    /** */ });

    connect(action2, &QAction::triggered, this, [this, edit]()
            /**/
            {
        QFileDialog *dialog = new QFileDialog(this);
        QString filename = dialog->getOpenFileName(this, "Load", "C:\\Users\\lenovo1\\Desktop>");
        qDebug("filename: %s", filename.toStdString().c_str());

        std::ifstream file(filename.toStdString().c_str());
        if (!file.is_open())
        {
            qDebug("file load failed");
            return;
        }
        std::string content, line;
        while (std::getline(file, line))
        {
            content += line;
            content += '\n';
        }
        file.close(); 
        QString text = QString::fromStdString(content);
        edit->setPlainText(text); 
    /** */ });
}

MainWindow::~MainWindow()
{
    delete ui;
}
